<%@ include file="Common.jsp" %><%!
//
//   Filename: Matches.jsp
//

static final String sFileName = "Matches.jsp";
              
%><%

String cSec = checkSecurity(1, session, response, request);
if ("sendRedirect".equals(cSec) ) return;
                
boolean bDebug = false;

String sAction = getParam( request, "FormAction");
String sForm = getParam( request, "FormName");
String sMatchesErr = "";
String sSearchErr = "";

java.sql.Connection conn = null;
java.sql.Statement stat = null;
String sErr = loadDriver();
conn = cn();
stat = conn.createStatement();
if ( ! sErr.equals("") ) {
 try {
   out.println(sErr);
 }
 catch (Exception e) {}
}

%>            
<html>
<head>
<title>Zoccer, das Fussball Tippspiel - Begegnungen</title>

</head>
<body style="background-color: #FFFFFF; color: #000000; font-family: Arial, Tahoma, Verdana, Helveticabackground-color: #FFFFFF; color: #000000; font-family: Arial, Tahoma, Verdana, Helvetica">
<jsp:include page="Header.jsp" flush="true"/><center>
 
 
<% Matches_Show(request, response, session, out, sMatchesErr, sForm, sAction, conn, stat); %>
    

<jsp:include page="Footer.jsp" flush="true"/>
</body>
</html>
<%%>
<%
if ( stat != null ) stat.close();
if ( conn != null ) conn.close();
%>
<%!

  void Matches_Show (javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, javax.servlet.http.HttpSession session, javax.servlet.jsp.JspWriter out, String sMatchesErr, String sForm, String sAction, java.sql.Connection conn, java.sql.Statement stat) throws java.io.IOException  {
  
    String sWhere = "";
    int iCounter=0;
    int iPage = 0;
    boolean bIsScroll = true;
    boolean hasParam = false;
    String sOrder = "";
    String sSQL="";
    String transitParams = "";
    String sQueryString = "";
    String sPage = "";
    int RecordsPerPage = 8;
    String sSortParams = "";
    String formParams = "";
    String pname="";

    transitParams = "type=" + toURL(getParam( request, "type")) + "&";
    formParams = "type=" + toURL(getParam( request, "type")) + "&"; 
    
    
    // Build ORDER statement
    String sSort = getParam( request, "FormMatches_Sorting");
    String sSorted = getParam( request, "FormMatches_Sorted");
    String sDirection = "";
    String sForm_Sorting = "";
    int iSort = 0;
    try {
      iSort = Integer.parseInt(sSort);
    }
    catch (NumberFormatException e ) {
      sSort = "";
    }
    if ( iSort == 0 ) {
      sForm_Sorting = "";
    }
    else {
      if ( sSort.equals(sSorted)) { 
        sSorted="0";
        sForm_Sorting = "";
        sDirection = " DESC";
        sSortParams = "FormMatches_Sorting=" + sSort + "&FormMatches_Sorted=" + sSort + "&";
      }
      else {
        sSorted=sSort;
        sForm_Sorting = sSort;
        sDirection = " ASC";
        sSortParams = "FormMatches_Sorting=" + sSort + "&FormMatches_Sorted=" + "&";
      }
    
      if ( iSort == 1) { sOrder = " order by matches.match_id" + sDirection; }
      if ( iSort == 2) { sOrder = " order by matches.timeout" + sDirection; }
      if ( iSort == 3) { sOrder = " order by matches.home" + sDirection; }
      if ( iSort == 4) { sOrder = " order by matches.guest" + sDirection; }
      if ( iSort == 5) { sOrder = " order by matches.tgroup" + sDirection; }
    }

      String tmpUID = (String) session.getAttribute("UserID");

      sSQL = "SELECT distinct matches.match_id as m_matchid, " +
	"matches.home as m_home, " +
	"matches.guest as m_guest, " +
	"matches.timeout as m_time, " +
	"matches.tgroup as m_group, " +
	"matches.home_score as m_homescore, " +
	"matches.guest_score as m_guestscore, " +
	"bets.bet_id as m_betid, " +
	"bets.score as m_score, " +
	"bets.home_guess as m_hometip, " +
	"bets.guest_guess as m_guesttip " +
	"FROM matches LEFT JOIN bets ON matches.match_id = bets.match_id" + " " +
	"and bets.member_id = " + toSQL(tmpUID, adNumber) + " " +
	"WHERE matches.match_type = " + getParam(request, "type");

      sSQL = sSQL + sOrder;


  String sNoRecords = "<tr>\n<td colspan=\"9\" style=\"background-color: #006600\"><font style=\"font-size: 10pt; color: #ffffff\">Keine Eintr&auml;ge</font></td>\n</tr>";

  String tableHeader = "";
      tableHeader = "<tr>\n"+
      "<td style=\"background:rgb(235,235,235)\"><font style=\"font-size: 10pt; color: #000000; font-weight: bold\">Zocken</font></td>\n" +
      "<td style=\"background:rgb(235,235,235)\"><a href=\""+sFileName+"?"+formParams+"FormMatches_Sorting=1&FormMatches_Sorted="+sSorted+"&\"><font style=\"font-size: 10pt; color: #000000; font-weight: bold\">Spiel-Nr.</font></a></td>\n" +
      "<td style=\"background:rgb(235,235,235)\"><a href=\""+sFileName+"?"+formParams+"FormMatches_Sorting=5&FormMatches_Sorted="+sSorted+"&\"><font style=\"font-size: 10pt; color: #000000; font-weight: bold\">Gruppe</font></a></td>\n " +
      "<td width=\"255\" style=\"background:rgb(235,235,235)\"><a href=\""+sFileName+"?"+formParams+"FormMatches_Sorting=2&FormMatches_Sorted="+sSorted+"&\"><font style=\"font-size: 10pt; color: #000000; font-weight: bold\">Anstoss</font></a></td>\n " + 
      "<td style=\"background:rgb(235,235,235)\"><a href=\""+sFileName+"?"+formParams+"FormMatches_Sorting=3&FormMatches_Sorted="+sSorted+"&\"><font style=\"font-size: 10pt; color: #000000; font-weight: bold\">Heim</font></a></td>\n " + 
      "<td style=\"background:rgb(235,235,235)\"><a href=\""+sFileName+"?"+formParams+"FormMatches_Sorting=4&FormMatches_Sorted="+sSorted+"&\"><font style=\"font-size: 10pt; color: #000000; font-weight: bold\">Gast</font></a></td>\n " +      
      "<td width=\"70\" style=\"background:rgb(235,235,235)\"><font style=\"font-size: 10pt; color: #000000; font-weight: bold\">Tipp</font></a></td>\n " +
      "<td width=\"70\" style=\"background:rgb(235,235,235)\"><font style=\"font-size: 10pt; color: #000000; font-weight: bold\">Ergebnis</font></a></td>\n " +
      "<td style=\"background:rgb(235,235,235)\"><font style=\"font-size: 10pt; color: #000000; font-weight: bold\">Punkte</font></a></td>\n " +
      "</tr>";
  
  try { 
      
    out.println("&nbsp;<br><br>");
    out.println("<table border=\"0\">");      
    out.print("<tr>");      
    // Set URLs            
    String fldField1 = "Matches.jsp?type=" + MATCHES_TYPE_GROUPMATCH;
    String fldField2 = "Matches.jsp?type=" + MATCHES_TYPE_FINALMATCH;
    String fldField3 = "Bets.jsp";   
    // Show fields                      
    out.print("<td><a href=\""+fldField1+"\"><font style=\"font-size: 12pt; color: #000000\"><b>Vorrunde</b></font></a>");
    out.print("<font style=\"font-size: 12pt; color: #000000\"><b> | </b></font></td>");
    out.print("<td><a href=\""+fldField2+"\"><font style=\"font-size: 12pt; color: #000000\"><b>Endrunde</b></font></a>");
    out.print("<font style=\"font-size: 12pt; color: #000000\"><b> | </b></font></td>");
    out.print("<td><a href=\""+fldField3+"\"><font style=\"font-size: 12pt; color: #000000\"><b>Wetten</b></font></a>");
    out.println("</tr></table>");
    
    out.println("&nbsp<br>");
    out.println("<table width=\"70%\" bgcolor=\"#000000\" cellspacing=\"1\" cellpadding=\"1\">");
    if (((String)getParam(request, "type")).equals("0")){
    	out.println("<tr>\n" +
    	 	"<td style=\"background:rgb(235,235,235); text-align: Center\" colspan=\"9\"><a name=\"Matches\"><font style=\"font-size: 12pt; font-weight: bold\">Vorrunden Begegnungen</font></a>" +
    		"</td>\n     </tr>");
    } else {
    	out.println("<tr>\n" +
    	 	"<td style=\"background:rgb(235,235,235); text-align: Center\" colspan=\"9\"><a name=\"Matches\"><font style=\"font-size: 12pt; font-weight: bold\">Endrunden Begegnungen</font></a>" +
    		"</td>\n     </tr>");
    }
    out.println(tableHeader);
  }
  catch (Exception e) {}

  
  try {
    // Select current page
    iPage = Integer.parseInt(getParam( request, "FormMatches_Page"));
  }
  catch (NumberFormatException e ) {
    iPage = 0;
  }

  if (iPage == 0) { iPage = 1; }
  //RecordsPerPage = 20;
  try {
    java.sql.ResultSet rs = null;
    // Open recordset
    rs = openrs( stat, sSQL);
    iCounter = 0;
    absolute (rs, (iPage-1)*RecordsPerPage+1);
    java.util.Hashtable rsHash = new java.util.Hashtable();
    String[] aFields = getFieldsName( rs );

    // Show main table based on recordset
    while ( (iCounter < RecordsPerPage) && rs.next() ) {

      getRecordToHash( rs, rsHash, aFields );
      String fldId = (String) rsHash.get("m_matchid");
      String fldTime = (String) rsHash.get("m_time");
      String fldHome = (String) rsHash.get("m_home");
      String fldGuest = (String) rsHash.get("m_guest");
      String fldGroup = (String) rsHash.get("m_group");
      String fldHometip = (String) rsHash.get("m_hometip");
      String fldGuesttip = (String) rsHash.get("m_guesttip");
      String fldHomescore = (String) rsHash.get("m_homescore");
      String fldGuestscore = (String) rsHash.get("m_guestscore");
      String fldPoints = (String) rsHash.get("m_score");
      String fldBetid = (String) rsHash.get("m_betid");
      //Get lookups
      String fldHomeLU = (String) dLookUp(stat, "teams", "name", "team_id=" + fldHome);
      String fldGuestLU = (String) dLookUp(stat, "teams", "name", "team_id=" + fldGuest);
      
      out.println("<tr>");
      
      out.print("<td style=\"background:rgb(235,235,235)\"><center>");
      
      if (isTimedOut(rs.getTimestamp("m_time"))) {
      	out.print("<img border=border:solid 1px  src=\"../images/tn_ball_cross.gif\">");
      } else {
        out.print("<a href=\"MatchBets.jsp?match_id=" + toURL((String)rsHash.get("m_matchid")) + "&bet_id=" + toURL((String)rsHash.get("m_betid")) + 
      		"&type=" + getParam(request, "type") + "&FromPage=" + new Integer(iPage).toString() + "\"><img border=border:solid 1px  src=\"../images/tn_ball.gif\"></a>");
      }
      
      out.println("</center></td>");      
      
      out.print("<td style=\"background-color: #ffffff\"><center>");
      out.print("<font style=\"font-size: 10pt; color: #000000\"><b>"+toHTML(fldId)+"&nbsp;</b></font>");
      out.println("</center></td>");
      
      out.print("<td style=\"background-color: #ffffff\"><center>");
      out.print("<font style=\"font-size: 10pt; color: #000000\"><b>"+toHTML(fldGroup)+"&nbsp;</b></font>");
      out.println("</center></td>");
      
      out.print("<td style=\"background-color: #ffffff\">");
      out.print("<font style=\"font-size: 10pt; color: #000000\"><b>"+toHTML(fldTime)+"&nbsp;</b></font>");
      out.println("</td>");
      
      out.print("<td width=\"250\" style=\"background-color: #ffffff\">");
      out.print("<font style=\"font-size: 10pt; color: #000000\"><b>&nbsp;<img src=\"../images/"+ fldHomeLU +".gif\"> "+toHTML(fldHomeLU)+"&nbsp;</b></font>");
      out.println("</td>");
      
      out.print("<td width=\"250\" style=\"background-color: #ffffff\">");
      out.print("<font style=\"font-size: 10pt; color: #000000\"><b>&nbsp;<img src=\"../images/"+ fldGuestLU +".gif\"> "+toHTML(fldGuestLU)+"&nbsp;</b></font>");
      out.println("</td>");
            
      out.print("<td style=\"background-color: #ffffff\"><center>");
      if (isEmpty(fldHometip) || isEmpty(fldGuesttip)){
      	out.print("<font style=\"font-size: 10pt; color: #000000\"><b>-</b></font>");
      } else {
      	out.print("<font style=\"font-size: 10pt; color: #000000\"><b>" + fldHometip + ":" + fldGuesttip + "</b></font>");
      }
      out.println("</center></td>");
      
      out.print("<td style=\"background-color: #ffffff\"><center>");
      if (isEmpty(fldHomescore) || isEmpty(fldGuestscore)){
      	out.print("<font style=\"font-size: 10pt; color: #000000\"><b>-</b></font>");
      } else {
      	out.print("<font style=\"font-size: 10pt; color: #000000\"><b>" + fldHomescore + ":" + fldGuestscore + "</b></font>");
      }
      out.println("</center></td>");
      
      out.print("<td style=\"background-color: #ffffff\"><center>");
      if (isEmpty(fldHomescore) || isEmpty(fldGuestscore) || isEmpty(fldGuesttip) || isEmpty(fldHometip)){
      	out.print("<font style=\"font-size: 10pt; color: #000000\"><b>-</b></font>");
      } else {
      	out.print("<font style=\"font-size: 10pt; color: #000000\"><b>"+toHTML(fldPoints)+"&nbsp;</b></font>");
      }
      out.println("</center></td>");
      
      out.println("</tr>");
    
      iCounter++;
    }
    if (iCounter == 0) {
      // Recordset is empty
      out.println(sNoRecords);
    
      iCounter = RecordsPerPage+1;
      bIsScroll = false;
    }

    else {

  
    // Parse scroller
    boolean bInsert = false;
    boolean bNext = rs.next();
    if ( !bNext && iPage == 1 ) {
    
    }
    else {
      out.print("<tr>\n<td colspan=\"9\" style=\"background:rgb(235,235,235)\"><font style=\"font-size: 12pt; color: #000000; font-weight: bold\">");
    
      if ( iPage == 1 ) {
        out.print("\n       <a href_=\"#\"><font style=\"font-size: 12pt; color: #000000; font-weight: bold\">Previous</font></a>");
      }
      else {
        out.print("\n       <a href=\""+sFileName+"?"+formParams+sSortParams+"FormMatches_Page="+(iPage - 1)+"#Form\"><font style=\"font-size: 12pt; color: #000000; font-weight: bold\">Previous</font></a>");
      }
  
      out.print("\n       [ "+iPage+" ]");
  
      if (!bNext) { 
        out.print("\n       <a href_=\"#\"><font style=\"font-size: 12pt; color: #000000; font-weight: bold\">Next</font></a><br>");
      }
      else {
        out.print("\n       <a href=\""+sFileName+"?"+formParams+sSortParams+"FormMatches_Page="+(iPage + 1)+"#Form\"><font style=\"font-size: 12pt; color: #000000; font-weight: bold\">Next</font></a><br>");
      }
    
      out.println("</td>\n     </tr>");
    }
  
    }

    if ( rs != null ) rs.close();
    out.println("    </table> &nbsp <br><br>");
    out.println("<br>");
    out.println(getRandomCit() + "<br>");
    
  }
  catch (Exception e) { out.println(e.toString()); }
}

%>